package com.example.botechotext.stream;

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class EventProcessor {
    private final Map<String, Runnable> handlers = new HashMap<>();

    public EventProcessor() {
        handlers.put("conversation.chat.created", this::handleChatCreated);
        handlers.put("conversation.chat.in_progress", this::handleChatInProgress);
        handlers.put("conversation.message.delta", this::handleMessageDelta);
        handlers.put("conversation.message.completed", this::handleMessageCompleted);
        handlers.put("conversation.chat.completed", this::handleChatCompleted);
        handlers.put("conversation.chat.failed", this::handleChatFailed);
        handlers.put("conversation.chat.requires_action", this::handleChatRequiresAction);
        handlers.put("error", this::handleError);
        handlers.put("done", this::handleDone);
    }

    private void handleError() {
    }

    private void handleChatRequiresAction() {
    }

    private void handleChatFailed() {
    }

    private void handleChatCompleted() {
    }

    private void handleMessageCompleted() {

    }

    private void handleMessageDelta() {
    }

    public void processEvent(StreamEvent event, StringBuilder args) {
        if ("conversation.message.completed".equals(event.getEventType())) {
            Object eventData = event.getEventData();
            JSONObject eventDataJson = JSONObject.parseObject(eventData.toString());
            Object object = eventDataJson.get("content");
            Object type = eventDataJson.get("type");
            switch (type.toString()) {
                case "query":
//                    args.append("用户输入内容:");
//                    args.append(object);
                    break;
                case "answer":
//                    args.append("bot回答：");

                    args.append(object);
                    break;
                case "function_call":
                    args.append("Bot 对话过程中调用函数：");
                    args.append(object);
                    break;
                case "tool_output":
                    args.append("调用工具结果：");
                    args.append(object);
                    break;
                case "tool_response":
                    args.append("调用工具结果：");

                    args.append(object);
                    break;
                case "follow_up":
                    args.append("问题建议：");

                    args.append(object);
                    break;
                case "verbose":
                    JSONObject jsonObject = JSONObject.parseObject(object.toString());
                    Object msgType = jsonObject.get("msg_type");
                    if (msgType.toString().equals("generate_answer_finish")) {
//                        args.append("回复完成!");
                    }

                    break;
                default:
                    System.out.println("Unknown event type: " + type);
                    break;

            }
            args.append("\n");
        }
//        Runnable handler = handlers.get(event.getEventType());
//        if (handler != null) {
//            handler.run();
//        } else {
//            System.err.println("Unrecognized event type: " + event.getEventType());
//        }
    }

    private void handleChatCreated() {
        System.out.println("A new conversation has been created.");
    }

    private void handleChatInProgress() {
        System.out.println("The server is processing the conversation.");
    }

    // ... 其他事件处理器 ...

    private void handleDone() {
        System.out.println("The session's stream has ended normally.");
    }
}